package game.block.command;

import game.block.Constants;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.game.Board;
import game.block.tile.Tile;
import game.block.turn.Turn;

public class WindCommand extends Command implements Constants {

    private MovableEntity block;
    private Tile tile;
    private Tile prevTile;
    private Board board;
    private Turn currentTurn;

    public WindCommand(MovableEntity block, Tile prevTile, Tile tile, Board board, Turn currentTurn) {
        this.block = block;
        this.board = board;
        this.prevTile = prevTile;
        this.tile = tile;
        this.currentTurn = currentTurn;
    }

    @Override
    public void execute() {
        block.spinTo(tile, TURN_LENGTH);
        if (tile.containsTrap()) { // apply next traps effects when block slides
                                   // onto it
            for (TrapEntity trap : board.getTraps()) {
                if (tile.getEntities().contains(trap)) {
                    ((TrapEntity) trap).applyEffect(block, board, currentTurn, tile);
                }
            }
        }
        block.getCurrentTile().changeColor(COLOR.BLANK);
        block.setCurrentTile(tile);
        tile.add(block);
        prevTile.remove(block);

    }

    @Override
    public void undo() {
        block.spinTo(prevTile, TURN_LENGTH);
        block.getCurrentTile().changeColor(COLOR.BLANK);
        block.setCurrentTile(prevTile);
        tile.remove(block);
        prevTile.add(block);
        board.undo();
    }


}
